package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.util.LinkedHashMap;

import javax.swing.JLabel;
import javax.swing.JPanel;

import model.evenements.Combat;
import controleur.Config;
import controleur.eMenuOption;
import controleur.iTransitionControleur;

@SuppressWarnings("serial")
public class CombatView extends absGenericView
{
	protected Combat fight;
	private JPanel panel;
	private JLabel player;
	private JLabel npc;
	private JLabel curPlay;
	private JLabel lastActions[];
	private JLabel tourNPC;
	private JLabel fightEnded;
	private final MenuView gameOpt;
	private JPanel center;

	public CombatView(Combat c, Config cfg)
	{
		super("Combat", "", "", cfg);

		LinkedHashMap<String, eMenuOption> tmp = new LinkedHashMap<String, eMenuOption>();
		tmp.put("Attaquer", eMenuOption.transitionResult1);
		//tmp.put("Utiliser un objet", eMenuOption.transitionResult2);
		tmp.put("Prendre la fuite", eMenuOption.transitionResult3);

		this.fight = c;
		this.gameOpt = new MenuView("Combat !", "Que voulez vous faire?", tmp,
				this.fight, cfg);
		this.gameOptContainer = this.gameOpt.getContentPane();
		this.addKeyListener(this.gameOpt);
		this.panel.add(BorderLayout.SOUTH, this.gameOptContainer);
		this.gameOpt.setVisible(false);

		this.panel.add(BorderLayout.CENTER, this.center);
		this.add(this.panel);
	}

	@Override
	protected void init()
	{
		this.panel = new JPanel();
		this.center = new JPanel();
		this.panel.setLayout(new BorderLayout());
		this.center.setLayout(new FlowLayout());
		this.panel.setBackground(Color.BLUE);
		this.center.setBackground(Color.BLUE);
		this.player = new JLabel();
		this.npc = new JLabel();
		this.curPlay = new JLabel();
		this.tourNPC = new JLabel(
				"<html>Au tour de l'adversaire d'attaquer.<br>"
						+ "Appuyez sur ENTREE</html>");
		this.tourNPC.setFont(new Font("Georgia", 1, 60));

		this.lastActions = new JLabel[8];
		for (int i = 0; i < this.lastActions.length; i++)
		{
			this.lastActions[i] = new JLabel("");
			this.lastActions[i].setFont(new Font("Georgia", 1, 28));
			this.center.add(this.lastActions[i]);
			// panel.add(BorderLayout.CENTER, this.lastActions[i]);
		}
		this.fightEnded = new JLabel("");
		this.panel.add(BorderLayout.WEST, this.player);
		this.panel.add(BorderLayout.EAST, this.npc);
		this.panel.add(BorderLayout.NORTH, this.curPlay);
		// panel.add(BorderLayout.SOUTH,this.tourNPC);
		this.center.add(this.fightEnded);
		// panel.add(BorderLayout.CENTER, this.fightEnded);

	}

	public void updateView()
	{
		if (!this.fight.isFinished())
		{
			for (JLabel lastAction : this.lastActions)
				lastAction.setVisible(true);

			/* le combat est encore en cours */
			this.player.setText(this.fight.getPlayer().getName() + " Vie : "
					+ this.fight.getPlayer().getLife() + "/"
					+ this.fight.getPlayer().getLifeMax());
			this.player.setFont(new Font("Georgia", 1, 46));

			this.npc.setText(this.fight.getNpc().getName() + " Vie : "
					+ this.fight.getNpc().getLife() + "/"
					+ this.fight.getNpc().getLifeMax());
			this.npc.setFont(new Font("Georgia", 1, 46));

			this.curPlay.setFont(new Font("Georgia", 1, 54));
			this.curPlay.setText("Tour du joueur : "
					+ this.fight.getCurrentPlayer().getName());

			if (this.fight.getCurrentPlayer() == this.fight.getPlayer())
			{
				this.tourNPC.setVisible(false);
				this.panel.remove(this.tourNPC);
				this.panel.add(BorderLayout.SOUTH, this.gameOptContainer);
				this.gameOptContainer.setVisible(true);
			}
			else
			{
				this.panel.remove(this.gameOptContainer);
				this.panel.add(BorderLayout.SOUTH, this.tourNPC);
				this.tourNPC.setVisible(true);
			}
		}
		else
		{
			/* on termine le combat */
			this.gameOptContainer.setVisible(false);
			this.tourNPC.setVisible(false);
			this.curPlay.setVisible(false);
			this.player.setVisible(false);
			this.npc.setVisible(false);
			for (JLabel lastAction : this.lastActions)
				lastAction.setVisible(false);
			this.curPlay.setVisible(false);
			this.fightEnded.setFont(new Font("Georgia", 1, 60));
			this.fight.togglePlayer();
			this.fightEnded.setText("Le combat est fini. Victoire de "
					+ this.fight.getCurrentPlayer().getName());
		}
	}

	public void setLastAction(String action)
	{
		for (int i = this.lastActions.length - 1; i > 0; i--)
			this.lastActions[i].setText(this.lastActions[i - 1].getText());
		this.lastActions[0].setText(action);
	}

	/*
	 * public void keyPressed(KeyEvent e){ super.keyPressed(e); if
	 * (e.getKeyCode()==KeyEvent.VK_ENTER){ fight.choix(); } }
	 */
	public MenuView getGameOpt()
	{
		return this.gameOpt;
	}

	private final Container gameOptContainer;

	@Override
	public void setConfig(Config c)
	{
		this.panel.setBackground(c.bg);
		this.center.setBackground(c.bg);

		this.player.setForeground(c.fg);
		this.npc.setForeground(c.fg);
		this.curPlay.setForeground(c.fg);
		this.tourNPC.setForeground(c.fg);
		this.fightEnded.setForeground(c.fg);

		for (int i = this.lastActions.length - 1; i >= 0; i--)
			this.lastActions[i].setForeground(c.fg);
	}

	@Override
	protected iTransitionControleur getControleur()
	{
		return this.fight;
	}

}
